﻿using UnityEngine;
using System.Collections;

public class Magnet_Static : MonoBehaviour//静止磁体
{
    [Range(2f, 7f)]
    public float AttractionRange = 5f;
    [Range(1f, 20f)]
    public float AttractionFactor = 4f;
    [Range(10f, 40f)]
    public float Mass;
    public Transform RangeSprite;
    private void Awake()
    {
        RangeSprite.localScale = new Vector3(AttractionRange * 2f, AttractionRange * 2f, 1);
    }
    private void FixedUpdate()
    {
        Collider2D[] cols = Physics2D.OverlapCircleAll(transform.position, AttractionRange, Constants.LayerMask_Ferromagnetism | Constants.LayerMask_Magnet);
        foreach (Collider2D item in cols)
        {
            if (item.transform != transform)
            {
                Rigidbody2D rgd = item.GetComponent<Rigidbody2D>();
                Vector2 force = rgd.transform.position - transform.position;
                force = force / force.sqrMagnitude * Mass * AttractionFactor;
                if (rgd != null)
                    rgd.AddForce(-force);
            }
        }
        RangeSprite.position = transform.position;
    }
}
